I Think ∴ I Am

Java, Flex, RIA, AJAX, Flash, Life, Food, Kites…

Creating a dynamic tree in flex

Posted by vipuljhawar on March 28, 2008

Tree control is one of the most powerful control in Flex. You must have stumbled upon N number of articles explaining the tree control, where they pick up the data from an XML for the tree, but that does not really happen too often if you have a tree which is to be built dynamically as loading a new XML everytime to build a tree is a costly affair.

So, let’s create a tree in Flex using actionscript objects which may be serialized version of server side objects or you may want to create a tree using some business logic rather than some plain input text. So, we will create a TreeNode class in Actionscript which will represent each and every node in the tree. The advantage of working with node objects is that you can have different types of objects at different levels and also use the inheritance concept pretty nicely. So a basic node may be a Node with variables – id, name, type.  A subclass of this node may be a PersonNode with additional variables – age, sex etc… and you can go on like this creating trees with the apt information and you can display any kind of data just by clicking on the node by fetching that node from the tree.

The top level class will be TreeNode.

public class TreeNode {

     public var id : int;

     public var name : String;

     public var type : int;

     public var children : ArrayCollection;

     public function addChild(node : TreeNode) : void {

      if (this.children == null)

          this.children = new ArrayCollection();

          children.addItem(node);

     }
//Similarly you could have getNode();

}

A sublcass of this could be a DataTreeNode

public class DataTreeNode extends TreeNode {

     public var data : Object;    

}

Now you can simply create a tree in Flex, by creating a RootNode of type DataTreeNode.

var rootNode : DataTreeNode = new DataTreeNode();

rootNode.id = 0;

rootNode.name = “Root”;

rootNode.type = -1;

Now you can keep creating nodes of different types and keep adding to the rootNode. Just assign the rootNode to you tree as a dataProvider and there you are, the flex tree will display automatically. I have found this approach much better and easy as it allows easy extension and also allows me to do much more on when i click a treeNode. Also, this way you don’t solely rely on the Tree api in Flex to do something with your tree, you can manipulate or swift your tree in any manner you want and reap the benefits.

If you have better way of creating dynamic tree in flex do comment, i would be more than pleased to improve or use that for my project, with all the regard definitely :-).

9 Responses to “Creating a dynamic tree in flex”

  1. JordanC said

    This is great.. is there anyway you could put this in an example application so I could get a better feel for whow it works?

  2. saurabh narula said

    @JoranC
    sample code for you

    Class
    package com.tclass
    {
    import mx.collections.ArrayCollection;
    public class treenode
    {
    public var id : int;

    public var name : String;

    public var type : int;

    public var children : ArrayCollection;

    public function addChild(node : treenode) : void {
    if (this.children == null)
    this.children = new ArrayCollection();
    children.addItem(node);
    }
    }
    }

    vipul thanks for the class!!..it helped 🙂

  3. saurabh narula said

    i missed out the flex code

  4. sundar said

    I tried this code. object Name is displaying on the root.
    can anybody help me to avoid this.

  5. mitch_p said

    Hi.New to flex.

    Can you show me how you would use this to create a “Dynamically” hierarchical arraycollection created from a list of paths. for example.

    List (say array list) returned from a databse:

    ***array list of Paths ****
    one/two
    one/two/three
    one/two/four
    five
    six/seven

    Corresponding arraycollection to has a structure like;

    one
    —two
    —–three
    ——-four
    five
    six
    —seven

    thanks in advance.

  6. David said

    Make a var called “label” or rename the “name” var to “label”. You can also use the tree-controls labelFunction.

  7. Sushma said

    How this actionscript object is assigned to the flex tree data provider. Its printing as an object.

  8. […] https://vipuljhawar.wordpress.com/2008/03/28/creating-a-dynamic-tree-in-flex/   […]

Leave a comment